package redpacket.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import redpacket.dto.CreatePacketRequest;
import redpacket.dto.GrabRequest;
import redpacket.dto.GrabResponse;
import redpacket.mapper.RedPacketRecordMapper;
import redpacket.model.RedPacketRecord;
import redpacket.service.RedPacketService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/redpacket")
@RequiredArgsConstructor
public class RedPacketController {

    private final RedPacketService redPacketService;
    private final RedPacketRecordMapper recordMapper;

    @PostMapping("/create")
    public Map<String, Object> create(@RequestBody @Valid CreatePacketRequest req) {
        Long id = redPacketService.create(req);
        return Map.of("packetId", id);
    }

    @PostMapping("/grab")
    public GrabResponse grab(@RequestBody @Valid GrabRequest req) {
        return redPacketService.grab(req.getPacketId(), req.getUserId());
    }

    @GetMapping("/{id}/records")
    public List<RedPacketRecord> records(@PathVariable("id") Long packetId) {
        return recordMapper.listByPacketId(packetId);
    }
}
